﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Urination
{
    public class Urination:IUrination
    {
        public void AddUserUrinaiton(int userId, DateTime clientDatetime, ref Model.NutriLeafDBEntities context)
        {
  
            Model.Urination urine = new Model.Urination();
            urine.DateTimeStamp = clientDatetime;
            urine.UserID = userId;
            context.Urinations.AddObject(urine);
            context.SaveChanges();
        }

        public void AddUserUrination(Model.User user, DateTime clientDateTime, ref Model.NutriLeafDBEntities context)
        {
            Model.Urination urine = new Model.Urination();
            urine.User = user;
            urine.DateTimeStamp = clientDateTime;
            context.Urinations.AddObject(urine);
            context.SaveChanges();
        }

        public void RemoveUserUrination(int urinationId, ref Model.NutriLeafDBEntities context)
        {
         context.DeleteObject(context.Urinations.Where(p=>p.UrinationID==urinationId).First());
            context.SaveChanges();
        }

        public void RemoveUserUrination(Model.Urination urination, ref Model.NutriLeafDBEntities context)
        {

            context.DeleteObject(context.Urinations.Where(p => p.UrinationID ==urination.UrinationID).First());
            context.SaveChanges();
        }

        public List<DateTime> GetAllUrinationInADay(int userId, DateTime clientDateTime, ref Model.NutriLeafDBEntities context)
        {
            List<DateTime> d = new List<DateTime>();
            List<Model.Urination> urine=  context.Urinations.Where(p => p.UserID == userId && p.DateTimeStamp.CompareTo(clientDateTime) == 0).ToList();
          foreach (Model.Urination u in urine) {
              d.Add(u.DateTimeStamp);
          }
          return d;
        }

        public int GetUrinationCountInADay(int userId, DateTime clientDateTime, ref Model.NutriLeafDBEntities context)
        {
            
        
          return context.Urinations.Where(p => p.UserID == userId && p.DateTimeStamp.CompareTo(clientDateTime) == 0).Count();
        
        }

        public int GetUrinationCount(int userId, DateTime clientDateTime, DateTime clientDateTimeEnd, ref Model.NutriLeafDBEntities context)
        {

          
            return context.Urinations.Where(p => p.UserID == userId && p.DateTimeStamp>=clientDateTime && p.DateTimeStamp >= clientDateTimeEnd).Count();
        
        }
    }
}